package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper,Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增菜品，同时把欧尼对应的口味数据。
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        // 保存菜品的基本信息到菜品表dish中
        this.save(dishDto);

        // 保存菜品口味数据到 菜品口味表dish_flavor
        Long dishId = dishDto.getId();
        // 菜品口味，增加dish_id
        List<DishFlavor> flavors = dishDto.getFlavors();
/*
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
*/
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }


        dishFlavorService.saveBatch(flavors);//批量保存
    }


    /**
     * 根据id回显数据
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        // 查询当前菜品对应的口味信息，从dish_flavor表查询。
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(dishFlavors);

        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        // 更新dish表信息
        this.updateById(dishDto); // 会更新普通字段。

        // 更新dishFlavor表 -- >先清理当前菜品对应口味数据，再添加当前提交的口味数据。
        // delete from dish_flavor where dish_id = ???
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        // insert into dish_flavor values (???);
        List<DishFlavor> flavors = dishDto.getFlavors();
        // 还要在 dish_flavor 中添加dish_id
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 菜品批量删除。
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithFlavorAndWithSetmeal(List<Long> ids) {
        /**
         * 分析：关联的表：dish dish_flavor    setmeal_dish
         * 套餐中有该菜品，不能删除。 起售不能删除。 都成立-->删除两个表中数据，
         */
        // select count(*) from setmeal_dish where dish_id in ids;
        // select count(*) from dish where id in ids and status = 1;
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        dishQueryWrapper.in(Dish::getId,ids);
        dishQueryWrapper.eq(Dish::getStatus,1);
        int count1 = this.count(dishQueryWrapper);

        LambdaQueryWrapper<SetmealDish> setmealDishQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishQueryWrapper.in(SetmealDish::getDishId,ids);
        int count2 = setmealDishService.count(setmealDishQueryWrapper);
        if(count1 > 0) {
            throw new CustomException("该菜品还在启售，操作失败!");
        }
        if(count2>0){
            throw new CustomException("该商品还在套餐中，操作失败！");
        }
        // 删除dish 表
        this.removeByIds(ids);
        // 删除dish_flavor
        LambdaQueryWrapper<DishFlavor> dishFlavorQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorQueryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(dishFlavorQueryWrapper);

    }


}
